<template>
  <div class="hey-dib">
    <el-button
      type="primary"
      plain
      size="mini"
      style="margin-bottom: 5px"
      :disabled="item.disabled||false"
      @click="handleClick"
    >{{ item.name }}
    </el-button>
    <el-dialog
      :close-on-click-modal="false"
      custom-class="hey-dialog-center"
      title="审核"
      :visible.sync="confVisible"
      width="40%"
    >
      <el-form v-if="inited" ref="form" label-width="80px" :rules="rules" :model="confModel">
        <el-row :gutter="20" class="border-b mgb20">
          <el-col :span="24">
            <el-form-item label="审核结果" :prop="confFields.last_chk_stcd">
              <el-radio-group v-model="confModel[confFields.last_chk_stcd]">
                <el-radio
                  v-for="(item,index) of chkStatus"
                  :key="index"
                  style="padding:10px"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                v-model="confModel[confFields.last_chk_cnclsn_dsc]"
                type="textarea"
                :autosize="{ minRows: 5, maxRows: 5}"
                size="small"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetConf">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import base from '../mixin/base'

export default {
  name: 'HeyTableElButtonConf',
  components: {},
  mixins: [base],
  data() {
    return {
      chkStatus: [{
        label: '通过',
        value: '2'
      }, {
        label: '不通过',
        value: '3'
      }],
      confModel: {},
      confVisible: false,
      inited: false,
      rules: {
      }
    }
  },
  computed: {},
  created() {
    const operateParam_ = this.operateParam_
    const { confFields, confFields: { last_chk_stcd = 'last_chk_stcd', last_chk_cnclsn_dsc = 'last_chk_cnclsn_dsc' }} = operateParam_

    const operateParam__ = {
      validate: [{
        key: last_chk_stcd,
        value: '1',
        msg: '只有[待审核]的数据才能进行该操作',
        flag: true
      }]
    }
    this.setParam({ ...this.operateParam_, ...operateParam__, ...this.operateParam, ...this.item.operateParam }, true)
    this.confFields = confFields
    const confModel = this.confModel
    this.$set(confModel, last_chk_stcd, '')
    this.$set(confModel, last_chk_cnclsn_dsc, '')
    this.rules[last_chk_stcd] = [{ required: true, message: '审核状态不能为空' }]
    this.inited = true
  },
  mounted() {
  },
  methods: {
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const { operateParam_ } = this
          const { transId, fn, confFields } = operateParam_
          const last_chk_stcd = this.confModel[confFields.last_chk_stcd]
          const chkStatus = this.chkStatus.find(item => item.value === last_chk_stcd)
          const mnplt_tpcd = last_chk_stcd === '2' ? '05' : '06'
          const param = { ...this.genParam(), ...this.confModel, ...{ mnplt_tpcd }}
          for (const [key, value] of Object.entries(param)) {
            if (key.includes('_grp')) {
              for (const row of value) {
                row.last_chk_stcd = last_chk_stcd
              }
              break
            }
          }
          console.log('请求参数为：', param)
          this.$emit('showLoading', true)
          this.post(transId, param, { showLoading: false }).then(ret => {
            if (fn) {
              this.resetConf()
              fn.call(this, ret)
            } else {
              this.$info('审核' + chkStatus.label + '成功！', () => {
                this.$emit('reqData')
                this.resetConf()
              })
            }
          }).catch(_ => {
            this.resetConf()
          })
        }
      })
    },
    resetConf() {
      this.$emit('showLoading', false)
      this.$refs.form.resetFields()
      this.confVisible = !this.confVisible
      this.resetObj(this.confModel)
    },
    canOpen() {
      const { selected, item } = this
      const { selectType } = item
      return this.isSelected(selected, selectType)
    },
    validate() {
      const { selected, operateParam_ } = this
      const { isValid, validate } = operateParam_
      return !isValid || this.canOper(selected, validate)
    },
    operate() {
      if (!this.canOpen() || !this.validate()) {
        return
      }
      this.confVisible = !this.confVisible
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
